package com.vanzo.talkie2;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;


/**
 * Created by tisen on 2018/1/15.
 */

public class TalkieActivity extends Activity {

    private ColorTextView mode_switch_text;
    private ImageView mode_switch_img;

    private ColorTextView gbw_switch_text;
    private ImageView gbw_switch_img;

    private ColorTextView power_switch_text;
    private ImageView power_switch_img;

    private ImageSwitch modeSwitch;
    private ImageSwitch gbwSwitch;
    private ImageSwitch powerSwitch;

    private ColorTextView tfv_text;
    private ColorTextView rfv_text;
    private ColorTextView txc_text;
    private ColorTextView rxc_text;

    private View topSwitch;

    private FrequencyPicker tFrequencyPicker;
    private FrequencyPicker rFrequencyPicker;
    private CxcssPicker tCxcssPicker;
    private CxcssPicker rCxcssPicker;

    private View sqView;
    private TextView sq_text;
    private SeekBar sq_seekBar;

    private ImageView edit_enable_img;
    private ImageView set_img;
    private ImageView send_img;

    private ATHelper atHelper;
    private ProgressDialog progressDialog;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talkie);
//        atHelper = new ATHelper(this, (Application) getApplication());
        init();
    }


    private void init() {

//        progressDialog = ProgressDialog.show(this, null, "正在初始化", false, false);
//        connect.sendEmptyMessageDelayed(failCount, 1000);
//        atHelper.connect(new ATHelper.Callback() {
//            @Override
//            public void callback(String result) {
//                Log.i("zts", "result : " + result);
//                if (result.contains("DMOCONNECT")) {
//                    progressDialog.dismiss();
//                } else {
//                    progressDialog.dismiss();
//                }
//            }
//        });

        topSwitch = findViewById(R.id.top_switch);
        sqView = findViewById(R.id.sq_set);
        mode_switch_text = (ColorTextView) findViewById(R.id.mode_switch_text);
        mode_switch_img = (ImageView) findViewById(R.id.mode_switch_img);
        modeSwitch = new ImageSwitch(this, mode_switch_text, mode_switch_img);
        modeSwitch.setImgSrc(R.drawable.mic_image_off, R.drawable.mic_image_on);
        modeSwitch.setTextValue(R.string.mode_title, R.string.mode_value_1, R.string.mode_value_2);
        modeSwitch.setValue(false);

        gbw_switch_text = (ColorTextView) findViewById(R.id.gbw_switch_text);
        gbw_switch_img = (ImageView) findViewById(R.id.gbw_switch_img);
        gbwSwitch = new ImageSwitch(this, gbw_switch_text, gbw_switch_img);
        gbwSwitch.setImgSrc(R.drawable.mic_image_off, R.drawable.mic_image_on);
        gbwSwitch.setTextValue(R.string.gbw_title, R.string.gbw_value_1, R.string.gbw_value_2);
        gbwSwitch.setValue(false);

        power_switch_text = (ColorTextView) findViewById(R.id.power_switch_text);
        power_switch_img = (ImageView) findViewById(R.id.power_switch_img);
        powerSwitch = new ImageSwitch(this, power_switch_text, power_switch_img);
        powerSwitch.setImgSrc(R.drawable.mic_image_off, R.drawable.mic_image_on);
        powerSwitch.setTextValue(R.string.power_title, R.string.power_value_1, R.string.power_value_2);
        powerSwitch.setValue(false);

        tfv_text = (ColorTextView) findViewById(R.id.tfv_text);
        rfv_text = (ColorTextView) findViewById(R.id.rfv_text);
        txc_text = (ColorTextView) findViewById(R.id.txc_text);
        rxc_text = (ColorTextView) findViewById(R.id.rxc_text);

        tFrequencyPicker = new FrequencyPicker(this, tfv_text, R.string.tfv_text_value, "414.5000");
        rFrequencyPicker = new FrequencyPicker(this, rfv_text, R.string.rfv_text_value, "455.6250");

        tCxcssPicker = new CxcssPicker(this, txc_text, R.string.txc_text_value, "22");
        rCxcssPicker = new CxcssPicker(this, rxc_text, R.string.rxc_text_value, "39");

        sq_text = (TextView) findViewById(R.id.sq_text);
        sq_seekBar = (SeekBar) findViewById(R.id.sq_seek);

        sq_seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                setSpannableString(getString(R.string.sq_text_value), progress + "", sq_text);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        sq_seekBar.setProgress(2);
        set_img = (ImageView) findViewById(R.id.set_img);
        send_img = (ImageView) findViewById(R.id.send_img);
        edit_enable_img = (ImageView) findViewById(R.id.edit_enable_img);
        edit_enable_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setEdit(true);

            }
        });

        set_img.setOnClickListener(setGroupListener);
        send_img.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_UP) {
                    atHelper.setPtt(0, new ATHelper.Callback() {
                        @Override
                        public void callback(String result) {

                        }
                    });
                }
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    atHelper.setPtt(1, new ATHelper.Callback() {
                        @Override
                        public void callback(String result) {

                        }
                    });
                }
                return false;
            }
        });
        setEdit(false);
    }

    private View.OnClickListener setGroupListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setEdit(false);
        }
    };

    private void setEdit(boolean enabled) {
        topSwitch.setVisibility(enabled ? View.VISIBLE : View.GONE);
        edit_enable_img.setVisibility(!enabled ? View.VISIBLE : View.INVISIBLE);

        tfv_text.setEnabled(enabled);
        rfv_text.setEnabled(enabled);
        txc_text.setVisibility(enabled ? View.VISIBLE : View.GONE);
        rxc_text.setVisibility(enabled ? View.VISIBLE : View.GONE);
        sqView.setVisibility(enabled ? View.VISIBLE : View.GONE);
        set_img.setVisibility(enabled ? View.VISIBLE : View.INVISIBLE);
        send_img.setVisibility(!enabled ? View.VISIBLE : View.INVISIBLE);
    }

    private void setSpannableString(String front, String txt, TextView textView) {
        SpannableString spannableString = new SpannableString(front + txt);
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(getResources().getColor(R.color.text_span_color));
        spannableString.setSpan(colorSpan, front.length(), spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        textView.setText(spannableString);
    }
}
